package com.maidou.qiantai.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.maidou.qiantai.common.utils.util.WebUtils;
import com.maidou.qiantai.vo.member.MemberAddressVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maidou.qiantai.mapper.MemberAddressMapper;
import com.maidou.qiantai.domain.MemberAddress;
import com.maidou.qiantai.service.IMemberAddressService;

import javax.annotation.Resource;

/**
 * 收货地址Service业务层处理
 * 
 * @author maidou
 * @date 2024-03-13
 */
@Service
public class MemberAddressServiceImpl implements IMemberAddressService 
{
    @Resource
    private MemberAddressMapper memberAddressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public MemberAddress selectMemberAddressById(Long id)
    {
        return memberAddressMapper.selectMemberAddressById(id);
    }

    /**
     * 查询收货地址列表
     * 
     * @param memberAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<MemberAddress> selectMemberAddressList(MemberAddress memberAddress)
    {
        return memberAddressMapper.selectMemberAddressList(memberAddress);
    }

    /**
     * 新增收货地址
     * 
     * @param memberAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertMemberAddress(MemberAddress memberAddress)
    {
        memberAddress.setCreateTime(DateUtils.getNowDate());
        return memberAddressMapper.insertMemberAddress(memberAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param memberAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateMemberAddress(MemberAddress memberAddress)
    {
        memberAddress.setUpdateTime(DateUtils.getNowDate());
        return memberAddressMapper.updateMemberAddress(memberAddress);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteMemberAddressByIds(Long[] ids)
    {
        return memberAddressMapper.deleteMemberAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteMemberAddressById(Long id)
    {
        return memberAddressMapper.deleteMemberAddressById(id);
    }

    /**
     * 获取用户地址信息
     * @return
     */
    @Override
    public List<MemberAddressVo> selectMemberAddressVoList() {
        Long memberId = WebUtils.getMemberIdByAccessToken();
        MemberAddress memberAddress = new MemberAddress();
        memberAddress.setMemberId(memberId);
        List<MemberAddress> memberAddresses = memberAddressMapper.selectMemberAddressList(memberAddress);
        List<MemberAddressVo> collect = memberAddresses.stream().map(item -> {
            MemberAddressVo memberAddressVo = new MemberAddressVo();
            BeanUtils.copyProperties(item, memberAddressVo);
            return memberAddressVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 新增地址
     * @param memberAddressVo
     * @return
     */
    @Override
    public Integer insertMemberAddressVo(MemberAddressVo memberAddressVo) {
        Long memberId = WebUtils.getMemberIdByAccessToken();
        MemberAddress memberAddress = new MemberAddress();
        BeanUtils.copyProperties(memberAddressVo,memberAddress);
        memberAddress.setIsDelete(0);
        memberAddress.setMemberId(memberId);
        Integer defaultStatus = memberAddressVo.getDefaultStatus();
        if (defaultStatus==null){
            memberAddress.setDefaultStatus(1);
        }
        return memberAddressMapper.insertMemberAddress(memberAddress);
    }

    /**
     * 修改地址
     * @param memberAddressVo
     * @return
     */
    @Override
    public Integer updateMemberAddressVo(MemberAddressVo memberAddressVo) {
        MemberAddress memberAddress = new MemberAddress();
        BeanUtils.copyProperties(memberAddressVo,memberAddress);
        return memberAddressMapper.updateMemberAddress(memberAddress);
    }
}
